//
//  TLAVPlayerBottomBar.m
//  TLAVPlayerDemo
//
//  Created by Tianlong on 2017/6/29.
//  Copyright © 2017年 Tianlong. All rights reserved.
//

#import "TLAVPlayerBottomBar.h"
#import "Masonry.h"
#import "TLAVPlayerPublicFile.h"
#import "TLAVPlayerSlider.h"

@interface TLAVPlayerBottomBar ()<TLAVPlayerSliderDelegate>
/** 播放按钮 */
@property (nonatomic , weak) UIButton *playBtn;
/** 播放时间 */
@property (nonatomic , weak) UILabel *curLabel;
/** 进度条 */
@property (nonatomic , weak) TLAVPlayerSlider *slider;
/** 总时长 */
@property (nonatomic , weak) UILabel *amountLabel;
/** 全半屏按钮 */
@property (nonatomic , weak) UIButton *screenBtn;
@end

@implementation TLAVPlayerBottomBar

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupPlayBtn];
        [self setupCurrentTimeLabel];
        [self setupSlider];
        [self setupAmountLabel];
        [self setupScreenBtn];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if(self = [super initWithCoder:aDecoder]){
        
    }
    return self;
}

#pragma mark - 设置播放按钮
-(void)setupPlayBtn{
    UIButton *playBtn = [[UIButton alloc] init];
    self.playBtn = playBtn;
    [self.playBtn setTitle:@"暂停" forState:UIControlStateNormal];
    [self.playBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.playBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [self.playBtn addTarget:self action:@selector(playOrPause) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.playBtn];
    self.playing = YES;
}

-(void)playOrPause{
    self.playing = !self.playing;
    if ([self.delegate respondsToSelector:@selector(playerBottomBar:didClickedPlayBtn:)]) {
        [self.delegate playerBottomBar:self didClickedPlayBtn:self.playing];
    }
}

-(void)setPlaying:(BOOL)playing{
    _playing = playing;
    [self.playBtn setTitle:self.playing == YES ? @"暂停" : @"播放" forState:UIControlStateNormal];
}

#pragma mark - 设置当前时间label
-(void)setupCurrentTimeLabel{
    UILabel *curLabel = [[UILabel alloc] init];
    self.curLabel = curLabel;
    self.curLabel.textColor = [UIColor whiteColor];
    self.curLabel.font = [UIFont systemFontOfSize:10];
    self.curLabel.text = @"00:00";
    //self.curLabel.backgroundColor = [UIColor redColor];
    self.curLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.curLabel];
}

#pragma mark - 设置进度条
-(void)setupSlider{
    
    if (self.slider == nil) {
        TLAVPlayerSlider *slider = [[TLAVPlayerSlider alloc] init];
        self.slider = slider;

        /** 整条背景 */
        self.slider.progressBackgoundColor = [UIColor groupTableViewBackgroundColor];
        /** 缓存进度颜色 */
        self.slider.trackBackgoundColor = [UIColor blueColor];
        /** 播放的背景色 */
        self.slider.playProgressBackgoundColor = [UIColor redColor];
        self.slider.delegate = self;
        [self addSubview:self.slider];
    }
}

#pragma mark - TLAVPlayerSliderDelegate
-(void)tlAVPlayerSlider:(TLAVPlayerSlider *)slider beiginDraggingForCurTime:(float)curTime{
    if ([self.delegate respondsToSelector:@selector(playerBottomBar:beiginDraggingForCurTime:)]) {
        [self.delegate playerBottomBar:self beiginDraggingForCurTime:curTime];
    }
}

-(void)tlAVPlayerSlider:(TLAVPlayerSlider *)slider didDraggingForScale:(float)scale{
    if ([self.delegate respondsToSelector:@selector(playerBottomBar:didDraggingForScale:)]) {
        [self.delegate playerBottomBar:self didDraggingForScale:scale];
    }
}

-(void)tlAVPlayerSlider:(TLAVPlayerSlider *)slider endDraggingForCurTime:(float)curTime scale:(float)scale{
    if ([self.delegate respondsToSelector:@selector(playerBottomBar:endDraggingForCurTime:scale:)]) {
        [self.delegate playerBottomBar:self endDraggingForCurTime:curTime scale:scale];
    }
}

#pragma mark - 设置总时长label
-(void)setupAmountLabel{
    UILabel *amountLabel = [[UILabel alloc] init];
    self.amountLabel = amountLabel;
    self.amountLabel.textColor = [UIColor whiteColor];
    self.amountLabel.font = [UIFont systemFontOfSize:10];
    //self.amountLabel.text = @"99:99";
    //self.amountLabel.backgroundColor = [UIColor redColor];
    self.amountLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.amountLabel];
}

#pragma mark - 设置全屏按钮
-(void)setupScreenBtn{
    UIButton *screenBtn = [[UIButton alloc] init];
    self.screenBtn = screenBtn;
    [self.screenBtn setTitle:@"全屏" forState:UIControlStateNormal];
    [self.screenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.screenBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [self.screenBtn addTarget:self action:@selector(fullOrSmallScreen) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.screenBtn];
    self.isFullScreen = NO;
}

-(void)fullOrSmallScreen{
    self.isFullScreen = !self.isFullScreen;
    if ([self.delegate respondsToSelector:@selector(playerBottomBar:didClickedScreenBtn:)]) {
        [self.delegate playerBottomBar:self didClickedScreenBtn:self.isFullScreen];
    }
}

-(void)setIsFullScreen:(BOOL)isFullScreen{
    _isFullScreen = isFullScreen;
    [self.screenBtn setTitle:self.isFullScreen == YES ? @"半屏" : @"全屏" forState:UIControlStateNormal];
}

#pragma mark - 设置当前播放时间 、总时长、缓冲时长
-(void)setCurTime:(float)curTime{
    _curTime = curTime;
    [self.slider setCurTime:curTime];
    self.curLabel.text = [self durationStringWithTime:(int)self.curTime];
}

-(void)setAmountTime:(float)amountTime{
    _amountTime = amountTime;
    //设置进度条的最大值
    self.slider.amountTime = self.amountTime;
    self.amountLabel.text = [self durationStringWithTime:(int)self.amountTime];
}

-(void)setBufferTime:(float)bufferTime{
    _bufferTime = bufferTime;
    self.slider.bufferTime = bufferTime;
}

- (NSString *)durationStringWithTime:(int)time{
    // 获取分钟
    NSString* min = [NSString stringWithFormat:@"%02d", time / 60];
    // 获取秒数
    NSString* sec = [NSString stringWithFormat:@"%02d", time % 60];
    return [NSString stringWithFormat:@"%@:%@", min, sec];
}

#pragma mark - layoutSubviews
-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat leftM = 43;
    CGFloat playBtnW = 30;
    CGFloat btnLeftM = 10;
    CGFloat labelW = 35;
    WS(ws);
    [self.playBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(btnLeftM);
        make.centerY.equalTo(ws.mas_centerY);
        make.width.offset(playBtnW);
        make.height.offset(playBtnW);
    }];
    
    [self.curLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftM);
        make.centerY.equalTo(ws);
        make.height.equalTo(ws);
        make.width.offset(labelW);
    }];
    
    [self.amountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-leftM);
        make.centerY.equalTo(ws);
        make.height.equalTo(ws);
        make.width.offset(labelW);
    }];
    
    [self.screenBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-btnLeftM);
        make.centerY.equalTo(ws.mas_centerY);
        make.width.offset(playBtnW);
        make.height.offset(playBtnW);
    }];
    
    [self.slider mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.curLabel.mas_right).offset(btnLeftM*0.5);
        make.right.equalTo(ws.amountLabel.mas_left).offset(-btnLeftM*0.5);
        make.centerY.equalTo(ws.mas_centerY);
        make.height.offset(20);
    }];
}

@end
